package com.example.queryexpressdelivery_01.fragment;

import android.os.Bundle;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.queryexpressdelivery_01.common.HttpGetData;
import com.example.queryexpressdelivery_01.adapter.IndexRvAdapter;
import com.example.queryexpressdelivery_01.R;
import com.example.queryexpressdelivery_01.entity.CompanyQueryFragment_02;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CompanyQueryFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CompanyQueryFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private String sTitle;
    private String userid;
    private TextView te01;
    private Button bu01;
    private Button bu02;
    private RecyclerView re01;
    private EditText te02;

    public CompanyQueryFragment() {
        // Required empty public constructor
    }

    public CompanyQueryFragment(String sTitle, String userid) {
        this.sTitle = sTitle;
        this.userid = userid;
        Bundle d1 = new Bundle();
        d1.putString("param1", sTitle);
        d1.putString("id", userid);
        this.setArguments(d1);
    }

    public String getTitle() {
        return sTitle;
    }

    public String getUserid() {
        return userid;
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment CompanyQueryFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static CompanyQueryFragment newInstance(String param1, String param2) {
        CompanyQueryFragment fragment = new CompanyQueryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    private void clearRecyclerView() {
        IndexRvAdapter adapter = (IndexRvAdapter) re01.getAdapter();
        if (adapter != null) {
            adapter.setExpressList(new ArrayList<>());
            adapter.notifyDataSetChanged();
        }
    }



    public void onStart() {
        super.onStart();
        bu02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                te02.setText("");
                clearRecyclerView();
            }
        });

        bu01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String CompanyName = te02.getText().toString().trim();

                if (CompanyName.isEmpty()) {
                    te01.setText("快递公司名称为空");
                } else {
                    re01.setLayoutManager(new LinearLayoutManager(getContext()));
                    List<Map<String, String>> expressInfo = new ArrayList<>();
                    IndexRvAdapter adapter = new IndexRvAdapter();
                    adapter.setExpressList(expressInfo);
                    re01.setAdapter(adapter);

                    HttpGetData httpGetData = new HttpGetData(null, CompanyName, null);
                    String jsonResponse = httpGetData.QueryCompany();
                    Gson gson = new Gson();
                    //01
                    if (jsonResponse == null) {
                        te01.setText("请输入正确的快递公司名称");
                        clearRecyclerView();
                    } else {
                        CompanyQueryFragment_02 response = gson.fromJson(jsonResponse, CompanyQueryFragment_02.class);

                        if (response != null && response.getStatus().equals("200")) {  // 如果状态为200，说明单号数据正常
                            CompanyQueryFragment_02.Result result = response.getResult();
                            if (result != null && result.getZTO() != null) {
                                String ztoValue = result.getZTO();
                                if (ztoValue.equals("null")) {
                                    te01.setText("请输入正确的公快递司名称");
                                    clearRecyclerView();
                                } else {
                                    te01.setText("");

                                    Map<String, String> status = new ArrayMap<>();
                                    status.put("查询状态：", "正常");

                                    Map<String, String> ztoInfo = new ArrayMap<>();
                                    ztoInfo.put("快递公司全称：", ztoValue);

                                    expressInfo.add(status);
                                    expressInfo.add(ztoInfo);
                                    adapter.setExpressList(expressInfo);
                                }
                            } else {
                                te01.setText("请输入有效的快递公司名称");
                                clearRecyclerView();
                            }
                        } else {
                            te01.setText("请输入有效的快递公司名称");
                            clearRecyclerView();
                        }
                    }
                }
            }
        });




    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_company_query, container, false);

        initView(view);
        return view;
    }

    private void initView(View view) {
        te01 = (TextView) view.findViewById(R.id.te01);
        bu01 = (Button) view.findViewById(R.id.bu01);
        bu02 = (Button) view.findViewById(R.id.bu02);
        re01 = (RecyclerView) view.findViewById(R.id.re01);
        te02 = (EditText) view.findViewById(R.id.te02);
    }
}